package org.speed.jdk;

public class LeakyDemo {
	
	
	public long timeStamp = getNowTime();
	public int capacity; // 桶的容量
	public int rate; //水漏出的速度
	public int water; //当前水量(当前累积请求数)
	
	public boolean grant() {
		long now = getNowTime();
		water = (int) Math.max(0, water - (now - timeStamp) * rate); // 先执行漏水，计算剩余水量
		timeStamp = now;
		if ((water + 1) < capacity) {
			water += 1;
			return true;
		} else {
			return false;
		}
	}
	
	long getNowTime() {
		return System.currentTimeMillis();
	}
	
	public static void main(String[] args) {
		
	}

}
